﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Collections.Generic;

using Microsoft.AspNetCore.Mvc;

using ZhonTai.Admin.Core.Dto;
using ZhonTai.DynamicApi;
using ZhonTai.DynamicApi.Attributes;

using ZhonTai.Industry.Domain.ProductionLine;
using ZhonTai.Industry.Core.Consts;
using ZhonTai.Industry.Services.ProductionLine.Dto.Output;
using ZhonTai.Industry.Services.ProductionLine.Dto.Input;
using ZhonTai.Admin.Services;
using ZhonTai.Industry.Services.ProductionLine;
using ZhonTai.Admin.Domain.Dict;
using ZhonTai.Admin.Domain.User;
using ZhonTai.Admin.Services.User.Dto;

namespace SYH.Industry.Services.ProductionLine
{
    /// <summary>
    /// 产线配置服务
    /// </summary>
    [DynamicApi(Area = IndustryConsts.AreaName)]
    public class ProductionLineService : BaseService, IProductionLineService, IDynamicApi
    {
        private readonly IProductionLineRepository _productionLineRepository;

        public ProductionLineService(IProductionLineRepository productionLineRepository)
        {
            _productionLineRepository = productionLineRepository;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ProductionLineGetOutput> GetAsync(long id)
        {
            return await _productionLineRepository.GetAsync<ProductionLineGetOutput>(id);
        }

        /// <summary>
        /// 列表查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IEnumerable<ProductionLineGetListOutput>> GetListAsync(ProductionLineGetListInput input)
        {
            var list = await _productionLineRepository.Select
                .WhereIf(!string.IsNullOrEmpty(input.LineCode), a => a.LineCode == input.LineCode)
                .OrderByDescending(a => a.Id)
                .ToListAsync<ProductionLineGetListOutput>();
            return list;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageOutput<ProductionLineGetPageOutput>> GetPageAsync(PageInput<ProductionLineGetPageInput> input)
        {
            var filter = input.Filter;
            var list = await _productionLineRepository.Select
                .WhereDynamicFilter(input.DynamicFilter)
                .WhereIf(filter != null && !string.IsNullOrEmpty(filter.LineCode), a => a.LineCode == filter.LineCode)
                .Count(out var total)
                .OrderByDescending(c => c.Id)
                .Page(input.CurrentPage, input.PageSize)
                .ToListAsync<ProductionLineGetPageOutput>();
            //关联查询代码

            var data = new PageOutput<ProductionLineGetPageOutput> { List = list, Total = total };

            return data;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<long> AddAsync(ProductionLineAddInput input)
        {
            var entity = Mapper.Map<ProductionLineEntity>(input);
            var id = (await _productionLineRepository.InsertAsync(entity)).Id;

            return id;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task UpdateAsync(ProductionLineUpdateInput input)
        {
            var entity = await _productionLineRepository.GetAsync(input.Id);
            if (!(entity?.Id > 0))
            {
                throw ResultOutput.Exception("产线配置不存在！");
            }

            Mapper.Map(input, entity);
            await _productionLineRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<bool> DeleteAsync(long id)
        {
            return await _productionLineRepository.DeleteAsync(id) > 0;
        }

        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<bool> SoftDeleteAsync(long id)
        {
            return await _productionLineRepository.SoftDeleteAsync(id);
        }

        /// <summary>
        /// 批量软删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<bool> BatchSoftDeleteAsync(long[] ids)
        {
            return await _productionLineRepository.SoftDeleteAsync(ids);
        }

        /// <summary>
        /// 设置启用
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task SetEnableAsync(ProductionLineSetEnableInput input)
        {
            var entity = await _productionLineRepository.GetAsync(input.ProductLineId);
            entity.Enabled = input.Enabled;
            await _productionLineRepository.UpdateAsync(entity);
        }
    }
}